<template>
  <div style="width: 100%; height: 100vh; background-color: #ffaa50; overflow: hidden; display: flex; justify-content: center; align-items: center;">
    <div style="width: 400px; background-color: white; padding: 40px; border-radius: 12px; box-shadow: 0 0 20px rgba(0, 0, 0, 0.1); transition: transform 0.3s ease-in-out;">
      <div style="color: #333; font-size: 30px; text-align: center; padding: 20px 0; font-weight: bold;">欢迎注册</div>
      <el-form ref="form" :model="form" :rules="rules" size="normal">
        <el-form-item prop="username">
          <el-input :prefix-icon="User" v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input :prefix-icon="Lock" v-model="form.password" show-password placeholder="请输入密码" />
        </el-form-item>
        <el-form-item prop="confirm">
          <el-input :prefix-icon="Lock" v-model="form.confirm" show-password placeholder="请确认密码" />
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%; background-color: #ffaa50; border-color: #ffaa50; font-weight: bold; transition: background-color 0.3s ease-in-out;" type="primary" @click="register" @mouseover="hoverButton" @mouseleave="leaveButton">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { User, Lock } from "@element-plus/icons-vue";
import request from "@/utils/request";

export default {
  name: "AdminRegister",
  computed: {
    User() {
      return User;
    },
    Lock() {
      return Lock;
    }
  },
  data() {
    return {
      form: {},
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        confirm: [
          { required: true, message: '请确认密码', trigger: 'blur' },
        ],
      }
    }
  },
  methods: {
    register() {
      if (this.form.password !== this.form.confirm) {
        this.$message({
          type: "error",
          message: "两次输入的密码不一致，请重新输入"
        });
        return;
      }
      this.$refs['form'].validate((valid) => {
        if (valid) {
          request.post("/admin/register", this.form).then(res => {
            if (res.code === '0') {
              this.$message({
                type: "success",
                message: "注册成功"
              });
              this.$router.push("/AdminLogin");
            } else {
              this.$message({
                type: "error",
                message: res.msg
              });
            }
          });
        }
      });
    },
    hoverButton(event) {
      event.target.style.backgroundColor = '#ffcc80';
    },
    leaveButton(event) {
      event.target.style.backgroundColor = '#ffaa50';
    }
  }
}
</script>

<style scoped>
.el-input__inner {
  border-color: #ffaa50;
  border-radius: 8px;
  padding-left: 35px;
  transition: border-color 0.3s ease-in-out;
}

.el-input__inner:focus {
  border-color: #ffcc80;
}

.el-form-item__label {
  color: #333;
  font-weight: bold;
}

.el-button {
  border-radius: 8px;
  transition: all 0.3s ease-in-out;
}

.el-button:hover {
  transform: scale(1.05);
}

.el-button:active {
  transform: scale(0.95);
}

</style>